<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@include file="/include/common/gridbackhead.jsp"%>
<%!
private class PageAction extends BaseAction{
	private String param1 = "";
	private String param2 = "";
	
	//准备action的环境变量 每次请次都会执行此方法 可在此方法中初如化一些变量
	protected void prepareActionEnv() throws Exception{
		param1 = utils.getHrNum();
		param2 = utils.getChName();
	}
	
	//加載頁面默認數據 
	protected boolean onLoadDefault() throws Exception{
		form.SetParamValue("hrnum1",utils.getHrNum());//请假者
//		form.SetParamValue("qh","1");//列出未簽核
		return onSearch() ;
	}
	
	// 处理查询的驗證
	protected void onSearchValidate() throws Exception{
		super.onSearchValidate();
	}
	
	//处理查询的結果
	protected void onSearchRetrieveResult() throws Exception{
		String sql = onSearchGetSql();
		if(ret.isError())return;
		List list = new ArrayList();
		//設置返回結果
		ResultSet rs = utils.executePagingQuery2("grid1","info3",sql,20);
		while(rs.next()){
			BaseDataEntity rowData = new BaseDataEntity();
			rowData.FromResultSet(rs,true);
			//itemRender(rowData);//處理每一行的數據
			list.add(rowData);
		}
		ret.SetSearchResult(list);
	}
	
	//取出查詢語句
	protected String onSearchGetSql() throws Exception{
		StringBuilderEx whr =  new StringBuilderEx("1=1");
		
		//部門
		if(!form.isNullOrEmpty("org") && !form.isNullOrEmpty("orgtsn"))
			whr.Append(" and c.tsn in (%1$s)",
					utils.getSubHr003Tsn(form.gpsv("orgtsn"),"'",",",true));
		
		//請假者工號
		if(!form.isNullOrEmpty("hrnum1"))
			whr.Append(" and d.hrnum1 = '%1$s'",form.gpsv("hrnum1"));
		
		//中英文姓名
		if(!form.isNullOrEmpty("cename"))
			whr.Append(" and (a.chname like '%%%1$s%%' or a.staffid like '%%%1$s%%')",form.gpsv("cename"));
		
		if("xs".equals(form.getActionValue())){
			//列出需要審核資料 ,如果此選項被選中則需要設定 [審核者工號 ]與[列出未簽核]
			form.SetParamValue("checkHrnum",utils.getHrNum());
			form.SetParamValue("qh","1");
			whr.Append(" and d.tsn in (select  hr021tsn from  hr..hr021a where hrnum='%1$s'  or (hrnum in (select d.hrnum  from hr..hr010a  a,hr..hr005 b ,hr..hr006 c,hr..hr005 d,hr..hr006 e  where a.hr005Tsn2=b.tsn and  b.tsn=c.hr005tsn  and  c.defau=1 and c.enab<>0  and d.tsn=e.hr005tsn  and  e.defau=1 and e.enab<>0   and a.hr005tsn1=d.tsn and b.hrnum='1$s' ))  and  defau<>1  )",
					utils.getHrNum());
		}else if(!form.isNullOrEmpty("checkHrnum")){
			//審核者工號
			if(StringHelper.Compare("000034",form.gpsv("checkHrnum"),true) == 0){//江茜
				whr.Append(" and (d.tsn in (select  hr021tsn  from hr..hr021a  where  hrnum='000034'  ) or d.tsn in (select hr021tsn from hr..hr021f where hrnum8='') )");
			}else{
				whr.Append(" and d.tsn in (select  hr021tsn  from hr..hr021a  where  hrnum='%1$s')",
						form.gpsv("checkHrnum"));
			}
		}
		
		//假別
		if(!form.isNullOrEmpty("hr013tsn")){
			whr.Append(" and d.hr013tsn = '%1$s'",form.gpsv("hr013tsn"));
		}
		
		//填寫日期
		if(!form.isNullOrEmpty("sdate") && !form.isNullOrEmpty("edate")){
			whr.Append(" and convert(char(10),d.crtDay,111)>=convert(char(10),convert(datetime,'%1$s'),111) "
					+" and convert(char(10),d.crtDay,111)<=convert(char(10),convert(datetime,'%2$s'),111)",
					form.gpsv("sdate"),
					form.gpsv("edate"));
		}
		
		//計酬方式
		if(!form.isNullOrEmpty("jobCode")){
			whr.Append(" and a.jobcode = '%1$s'",form.gpsv("jobCode"));
		}
		
		//列出未簽核
		if(!form.isNullOrEmpty("qh")){
			whr.Append(" and  d.leaveOk = 0   and  d.enab=1 and d.tsn not in (select hr021tsn from hr..hr021a where defau=2) ");
		}
		
		//請假日期
		if(!form.isNullOrEmpty("qsdate") && !form.isNullOrEmpty("qedate")){
			whr.Append(" and d.tsn in (select hr021tsn from hr..hr021c where leaveday>='%1$s' and leaveday<='%2$s')",
					form.gpsv("qsdate"),
					form.gpsv("qedate"));
		}
		
		//hr021tsn
		if(!form.isNullOrEmpty("tsn")){
			whr.Append(" and d.tsn = '%1$s'",form.gpsv("tsn"));
		}
		
		//至少要有一個查詢條件
		if(whr.toString().length() <= 3){
			//ret.setRetCode(Errors.NOK);
			//ret.setErrorInfo("至少要有一個查詢條件!");
		}
		
		return StringHelper.Format("select d.tsn,d.hrNum1,d.Enab,a.ChName as cName,e.companyNameCh,d.leaveOk,c.ChFullName,f.leaveKind,convert(char(16),d.startDay,121) as startDay,convert(char(16),d.endDay,121) as endDay,d.crtDay,g.AddrCty,c.hr003atsn as hr003atsn1,b.hr003atsn as hr003atsn2 ,d.rem "
				+" from hr..hr005 a,hr..hr006 b ,hr..hr003 c,hr..hr021 d,hr..hr003a  e,hr..hr013 f ,hr..hr007 g   "
				+" where c.hr003atsn=e.tsn  and  b.defau=1  and a.hrnum =d.hrnum1  and  a.tsn=b.hr005tsn  and b.HR003Tsn=c.tsn and c.Enab<>0  and  d.hr013tsn=f.tsn  and  a.hr007tsn=g.tsn"
				+" and %1$s  order by d.crtDay desc",
				whr.toString());
	}
	
	//處理每一行的數據
	protected void itemRender(BaseDataEntity rowData) throws Exception{
	    boolean isAdmin2 	= utils.isAuthorized("staff040Q-2");//非業務及外派人員的假單修簽核改權限
	    boolean isAdmin3 	= utils.isAuthorized("staff040Q-3");//業務及外派人員的假單修改簽核權限
	    boolean isAdmin4 	= utils.isAuthorized("staff040Q-4");//除簽核人員外可以點進去看國外出差的人員
	    
		String hr021tsn 	= rowData.gpsv("tsn");//hr021 tsn
		String hrnum1		= rowData.gpsv("hrnum1");//hr021 tsn
		int hr003atsn1 		= rowData.GetParamIntValue("hr003atsn1",0);//請假人的公司別
	    int hr003atsn2 		= rowData.GetParamIntValue("hr003atsn2",0);//請假人的真實公司別
	    int leaveOk 		=  rowData.GetParamIntValue("leaveOk",0);//假單的簽核狀態
	    
	    //取出此筆資料其他相關信息
	    String days 		= "";//請假時長
	    int hr021bCnt 		= 0;//未確認的工作交接單數量
	    int hr021aNokCnt	= 0;//hr021a簽核不OK數量
	    int hr021aCnt 		= 0;//hr021a數量
	    String checkHrnum 	= "";//請假人對應一簽核人工號
	    int agentCnt 		= 0; //簽核人在hr010a中有設置代理人
	    
	    String bgcolor 		= "";//每一行的顏色
	    
	    StringBuilderEx sql = new StringBuilderEx();
	    sql.Append("\r\n select (select convert(decimal(12,1),sum(leaveHour/convert(decimal(12,1),(datediff(hh,startHour,endHour)-datediff(hh,startRest1,endRest1)-datediff(hh,startRest2,endRest2)-datediff(hh,startRest3,endRest3)))))    from  hr..hr021c  where  hr021tsn='%1$s') as days, ",hr021tsn);
	    sql.Append("\r\n (select count(*) from hr..hr021b  where hr021tsn='%1$s'  and  proxyConfirm=0 ) as hr021bCnt ,",hr021tsn);
	    sql.Append("\r\n (select count(*)  from hr..hr021a where hr021tsn='%1$s' and  defau=2) as hr021aNokCnt ,",hr021tsn);
	    sql.Append("\r\n (select count(*)  from hr..hr021a where hr021tsn='%1$s' ) as hr021aCnt, ",hr021tsn);
	    sql.Append("\r\n (select HRNum  from hr..hr021a where hr021tsn='%1$s' and   defau=0  and verifylev in (select  min(verifyLev) from hr..hr021a  where   hr021tsn='%1$s'   and   defau=0 )) as checkHrnum,",hr021tsn);
	    sql.Append("\r\n (select  count(*) from hr..hr021a  a,hr..hr005 b,hr..hr006 c,hr..hr010a  d,hr..hr005 e  where a.hr021tsn='%1$s' and a.defau=0  and a.verifylev in (select  min(verifyLev) from hr..hr021a  where   hr021tsn='%1$s'  and   defau=0 ) and a.hrnum=b.hrnum and b.tsn=c.hr005tsn and  c.defau=1 and c.enab<>0  and  b.tsn=d.hr005tsn1 and  d.hr005tsn2=e.tsn  and  e.hrnum='%1$s' ) as agentCnt",hr021tsn);
	    
	    ResultSet rs = utils.executeQuery("info3",sql.toString());
	    if(rs.next()) {
	    	days = utils.getRsString(rs,"days");//請假時長
	    	rowData.SetParamValue("days",days);
	    	
	    	hr021bCnt = rs.getInt("hr021bCnt");//未確認的工作交接單數量
	    	hr021aNokCnt =  rs.getInt("hr021aNokCnt");//hr021a簽核不OK數量
	    	hr021aCnt =  rs.getInt("hr021aCnt");//hr021a數量
	    	checkHrnum = utils.getRsString(rs,"checkHrnum");//請假人對應一簽核人工號
	        agentCnt = rs.getInt("agentCnt");//簽核人在hr010a中有設置代理人
        }   
	    rs.close();
	    
	   	//領導處理,黃襄湖(二董)
       	if("000003".equals(checkHrnum)) {
    	   	sql.Reset();
			sql.Append("select * from hr..hr021f where hr021tsn = '%1$s' and hrnum8 = ''",hr021tsn);
           	ResultSet rstest = utils.executeQuery("info3",sql.toString());
           	if(rstest.next()) {
        		checkHrnum = "999999";
           	}
       	}
	   	
	    boolean isgncc = false;		//國內出差是否有出差單,特指蘇州出差到沭陽,沭陽出差到蘇州
        String gncc_zgtsn = "";		//國內出差主管tsn
        String gncc_cztsn = "";		//國內出差處長tsn
        boolean gncc_zgok = false;  //國內出差主管是否簽核OK
        boolean gncc_czok = false;  //國內出差處理是否簽核OK
        
        boolean isgwcc = false;		//國外出差是否有出差單
        boolean gwcc_isok = false;	//國外出差是否已經結案
        
        //判斷是否有國內外出差
        sql.Reset();
        sql.Append("select * from hr..hr021h where hr021tsn='%1$s'",hr021tsn);
        rs = utils.executeQuery("info5",sql.toString());
        if(rs.next()) {
        	isgncc = true;
        	gncc_zgtsn = utils.getRsString(rs,"zgtsn");
        	gncc_cztsn = utils.getRsString(rs,"cztsn");
        	gncc_zgok = "".equals(utils.getRsString(rs,"zgchk"))?false:true;
        	gncc_czok = "".equals(utils.getRsString(rs,"czchk"))?false:true;
        }
        rs.close();
        
        if(!isgncc) {
        	//國外出差是否有出差單
        	sql.Reset();
            sql.Append("select * from hr..hr021m where hr021tsn='%1$s'",hr021tsn);
        	rs = utils.executeQuery("info5",sql.toString());
        	if(rs.next()) {
        	 	isgwcc = true;
        	 	if("4".equals(utils.getRsString(rs,"isok"))) {
        	 		gwcc_isok = true;
        	 	}
        	}
        	rs.close();
        }
        
        /***********以下處理欄位數據************/
     	//處理每一行的顏色
		if(checkHrnum.equals("1")) {
            if((hr003atsn1!=1 && hr003atsn2!=7 && isAdmin2) || (((hr003atsn1==1 && hr003atsn2==2) || hr003atsn2==7) && isAdmin3)) {
            	bgcolor = "#FFEDA6"; 
            } else {
            	//bgcolor = "#FFFFFF";
            }
        } else {
            if(checkHrnum.equals(utils.getHrNum()) || agentCnt>0) {
            	bgcolor = "#FFEDA6";     
            } else {
            	//bgcolor = "#FFFFFF";
            }       
        }
		rowData.SetParamValue("bgcolor",bgcolor);
		
        //處理工號欄位的編輯頁面
	    String strEditPage = "staff039E.jsp";
	    if((hr003atsn1==1 && hr003atsn2==2) || hr003atsn2==7) {
			//外派人員
	    	strEditPage = "staff134E.jsp";
		}
	    rowData.SetParamValue("editPage",
	    		StringHelper.Format("/jmPro/staff/%1$s?tsn=%2$s&HRNum1=%3$s",strEditPage,hr021tsn,hrnum1));
	    
        //處理簽核欄位
        String strCheckPage = "" ;
        if("999999".equals(checkHrnum)) {
        	//領導
        	strCheckPage = StringHelper.Format("/jmPro/staff/%1$s?tsn=%2$s&HRNum1=%3$s","staff041S.jsp",hr021tsn,hrnum1);
        }else if(
        		( 	( utils.getHrNum().equals(checkHrnum)//當前是簽核人 
        				|| agentCnt>0 //當前人是代理 人
        			) 
        			&&  hr021bCnt == 0 //無未確認的工作交接單
        		) 
        		|| isAdmin4 //除簽核人員外可以點進去看國外出差的人員
        		|| gncc_zgtsn.equals(utils.getHr005Tsn())//主管可簽出差單
        		|| gncc_cztsn.equals(utils.getHr005Tsn())//處長可簽出差單
        		)
        {
        	if(!isgwcc && !isgncc && (utils.getHrNum().equals(checkHrnum) || agentCnt>0) &&  hr021bCnt == 0) {
        		strCheckPage = StringHelper.Format("/jmPro/staff/%1$s?tsn=%2$s&HRNum1=%3$s","staff041E.jsp",hr021tsn,hrnum1);
        	}
        	/**
        	else if(isgncc && ((gncc_zgtsn.equals(utils.getHr005Tsn()) && !gncc_zgok && !gncc_czok) || (gncc_cztsn.equals(utils.getHr005Tsn()) && gncc_zgok && !gncc_czok))) {
        		//國內出差單 
        		strCheckPage = StringHelper.Format("/jmPro/staff/%1$s?tsn=%2$s&HRNum1=%3$s","staff534E.jsp",hr021tsn,hrnum1);        		
        	}
        	***/
        	else  if(isgwcc && !gwcc_isok) {
        		//國外出差單 
        		strCheckPage = StringHelper.Format("/jmPro/staff/%1$s?tsn=%2$s&HRNum1=%3$s","staff539E.jsp",hr021tsn,hrnum1);
        	}
        }else{
        	if("1".equals(checkHrnum) && ((hr003atsn1!=1 && hr003atsn2!=7 && isAdmin2) || (((hr003atsn1==1 && hr003atsn2==2) || hr003atsn2==7) && isAdmin3))){
        		strCheckPage = StringHelper.Format("/jmPro/staff/%1$s?tsn=%2$s&HRNum1=%3$s","staff041E.jsp",hr021tsn,hrnum1);
        	}
        }
        rowData.SetParamValue("checkPage",strCheckPage);
        
        //簽核狀態
        StringBuilderEx	status = new StringBuilderEx();;
        if(hr021bCnt == 0){
			if(leaveOk == 1 && hr021aCnt == 0) {//批量請假
				status.Append("<font color=#ff0000>批量SYS:OK</font>;");
        	}else{
        		sql.Reset();
        		//取出此筆假單的簽核狀態
        		sql.Append("select  b.verifyLev as ver,c.leaveOk, a.hrnum,chname=(a.chname+(case when a.chname=b.updName  or b.updName=''   then '' when a.chname<>b.updName and b.updName<>''   then   '('+b.updName+'代)'    end )),verifyLev=(case b.verifyLev when 1 then '一級' when 2 then '二級'   else ''  end ),defau=(case b.defau  when  0 then '簽核中' when 1 then 'OK' when 2 then '不OK'  end )  into #aa  from hr..hr005  a,hr..hr021a b ,hr..hr021  c  where  c.tsn=b.hr021tsn and  verifylev<>3 and  a.hrnum=b.hrnum   and  hr021tsn='%1$s'  order by verifyLev         insert into #aa  select distinct b.verifyLev as ver,a.leaveOk,hrnum=(case  isnull(c.hrnum,'')  when ''  then 'SYS'  else  isnull(c.hrnum,'')  end  ),hrnum=(case  isnull(c.chname,'')  when ''  then 'SYS'  else  isnull(c.chname,'')  end ),'人資'   as  verifylev,defau=(case b.defau when 0 then '簽核中'  when 1 then 'OK' when 2 then '不OK'  end)  from hr..hr021  a,hr..hr021a  b  left join  hr..hr005  c  on  b.hrnum=c.hrnum where  a.tsn=b.hr021tsn   and  b.verifyLev=3  and b.hr021tsn='%1$s'   select * from #aa order by ver",hr021tsn);
	            rs = utils.executeQuery("info3",sql.toString());
	            while(rs.next()) {
				String defau = utils.getRsString(rs,"defau");
				status.Append("%1$s:%2$s",
	            			utils.getRsString(rs,"verifyLev"),
	                    	utils.getRsString(rs,"chname")); 

	                if(defau.equals("簽核中")) {
	                	status.Append("<font color=#0000ff>%1$s</font>;<br>",defau);
	                } else if (defau.equals("OK")) {
	                	status.Append("<font color=#ff0000>%1$s</font>;<br>",defau);
					} else if(defau.equals("不OK")) {
						status.Append("<font color=#ff00ff>%1$s</font>;<br>",defau);
					} else {
						status.Append("<font color=#ffffff>無</font>;");
					}
				}
			}
        }else{
        	status.Append("<font color=#ff5500>交接單簽核中</font>;");
        }
        
        rowData.SetParamValue("status",status.toString());
        
        //crtDate 人資修改頁面
        String strRzEditPage = "" ;
        if((hr003atsn1!=1 && hr003atsn2!=7 && isAdmin2) || (((hr003atsn1==1 && hr003atsn2==2) || hr003atsn2==7) && isAdmin3)) {
        	strRzEditPage = StringHelper.Format("/jmPro/staff/%1$s?tsn=%2$s&HRNum1=%3$s","staff044E.jsp",hr021tsn,hrnum1);
        }
	}
	
	
	
	//处理自定義操作
	protected boolean onCustomAction(String action) throws Exception{
		if("v_hrnum1".equalsIgnoreCase(action)){
			onValidateHrnum();
		}
		return super.onCustomAction(action);
	}
	
	//ajax 验证工号
	protected void onValidateHrnum()  throws Exception{
		PageResult pageResult = ret.getCallResult();
		String hrnum = form.gpsv("hrnum");
		
		if(StringHelper.IsNullOrEmpty(hrnum)){
			pageResult.setRetCode(Errors.NOK);
		}else{
			ResultSet rs = utils.executeQuery("info3", StringHelper.Format("select count(*) as cnt from hr..HR005 where HRNum='%1$s'",hrnum));
			if(!rs.next() || rs.getInt("cnt") == 0) {
				pageResult.setRetCode(Errors.NOK);
			}
		}
		this.getPage().Output(pageResult.ToJSONString());
	}
}
%>
<%@include file="/include/common/gridbackend.jsp"%>